package com.springboot.demo.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wubc683
 * @desc
 * @date 2021-08-24 18:06
 */
//@Slf4j
//@Component
//public class TokenInterceptor implements HandlerInterceptor {
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception{
//        if(request.getMethod().equals("OPTIONS")){
//            response.setStatus(HttpServletResponse.SC_OK);
//            return true;
//        }
//        response.setCharacterEncoding("utf-8");
//
//        String token = request.getHeader("token");
//        if(token != null){
//            boolean result = TokenUtils.verify(token);
//            if(result){
//                System.out.println("通过拦截器");
//                return true;
//            }
//        }
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json; charset=utf-8");
//        PrintWriter out = null;
//        try{
//            Map json = new HashMap();
//            json.put("success","false");
//            json.put("msg","认证失败，未通过拦截器");
//            json.put("code","50000");
//            response.getWriter().append(json.toString());
//            System.out.println("认证失败，未通过拦截器");
//            //        response.getWriter().write("50000");
//        }catch (Exception e){
//            log.error("token verify failed");
//            response.sendError(500);
//            return false;
//        }
//        return false;
//    }
//}